<div class="section">
    <?php
    echo $this->element('toolbar');
    ?>
    <!-- Table -->
    <div class="table-wrapper">
        <?php echo $this->Form->create('Filter', array('secure' => false, 'url' => '/' . $this->request->url,'type' => 'get')); ?>        
        <div class="table-toolbar clearfix">
            <div class="input table-length">

            </div>
            <div class="table-filter">
                <?php
                echo __d('system', 'Theme %s', $this->Form->input('theme', array(
                            'value' => $themeName,                            
                            'id' => 'filter-limit', 'options' => array_combine($themes, $themes), 'div' => false, 'label' => false)));
                ?>
                <?php
                echo __d('system', 'Layout %s', $this->Form->input('layout', array(
                            'value' => $layoutName,                           
                            'id' => 'filter-limit', 'options' => array_combine($layouts, $layouts), 'div' => false, 'label' => false)));
                ?>
                <?php
                echo __d('system', 'Sub site %s', $this->Form->input('sub_site', array(                                        
                        'options' => $sub_site, 'div' => false, 'label' => false,'type'=>'select','empty'=>'----Chọn sub site---','default'=>$defaultSubSite)));
                ?>                            
            </div>            
        </div>        
        <div id="table-options" style="display:none" class="table-options panel clearfix"></div>

        <?php echo $this->Form->end(); ?>
        <?php echo $this->Form->create('Paginate', array('id' => 'paginate', 'url' => $this->request->here(false), 'type' => 'post', 'inputDefaults' => array('label' => false))); ?>                          
        <div class="paginate-place">
            <div class="table-scroll">
                <div class="table-content">
                    <table>
                        <thead>
                            <tr>
                                <th>
                                    <span class="table-head">
                                        <span class="ui-icon ui-icon-locked"></span>
                                        <?php echo __d('system', 'Block'); ?>
                                    </span>
                                </th>

                                <th>
                                    <span class="table-head">
                                        <span class="ui-icon ui-icon-calculator"></span>
                                        <?php echo __d('system', 'Region'); ?>
                                    </span>
                                </th>

                                <th>
                                    <span class="table-head">
                                        <span class="ui-icon ui-icon-clipboard"></span>
                                        <?php echo __d('system', 'Weight'); ?>
                                    </span>
                                </th>

                                <?php if (isset($_translateMaps)): ?>
                                    <th>    
                                        <span class="table-head">
                                            <span class="ui-icon ui-icon-gear"></span>
                                            <?php echo __d('system', 'Translate'); ?>                              
                                        </span>
                                    </th>
                                <?php endif; ?>

                                <th>
                                    <span class="table-head">
                                        <span class="ui-icon ui-icon-gear"></span>
                                        <?php echo __d('system', 'Action'); ?>
                                    </span>
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $_row = 1;
                            $defaultLanguage = Router::defaultLanguage();
                            $_queries = array('continue' => $this->request->here(false));

                            $option = range(-(ceil($blockCount / 2)), $blockCount);
                            $option = array_combine($option, $option);
                            ?>
                            <?php foreach ($regions as $key => $block) : ?>
                                <tr class="table-row table-region-title">
                                    <td class="table-col" colspan="5">
                                        <strong><?php echo h($block['name']); ?></strong>
                                    </td>
                                </tr>
                                <?php $_row = 1; ?>
                                <?php if (empty($block['blocks'])): ?>
                                    <tr class="table-row <?php echo $_row % 2 == 0 ? 'even' : 'odd'; ?>">
                                        <td class="table-col table-block-title" colspan="5">
                                            <i><?php echo __d('system', 'No blocks in this region'); ?></i>
                                        </td>
                                    </tr> 
                                <?php else: ?>
                                    <?php foreach ($block['blocks'] as $value) : ?>
                                        <?php $rowName = 'Block.' . $value['id'] . '.'; ?>
                                        <tr class="table-row <?php echo $_row % 2 == 0 ? 'even' : 'odd'; ?>">
                                            <td class="table-col table-block-title">
                                                <i><?php echo h($value['description']); ?></i>
                                            </td>
                                            <td class="table-col table-string">
                                                <?php
                                                echo $this->Form->hidden($rowName . 'id', array(
                                                    'value' => $value['id']
                                                ));
                                                echo $this->Form->input($rowName . 'region', array(
                                                    'type' => 'select',
                                                    'options' => $themeRegions,
                                                    'label' => false,
                                                    'value' => $key
                                                ));
                                                ?>
                                            </td>

                                            <td class="table-col table-string">
                                                <?php
                                                echo $this->Form->input($rowName . 'weight', array(
                                                    'type' => 'select',
                                                    'options' => $option,
                                                    'label' => false,
                                                    'value' => $value['weight']
                                                ));
                                                ?>
                                            </td>

                                            <?php if (isset($_translateMaps)): ?>
                                                <td class="table-col">
                                                    <div class="table-translate">
                                                        <ul class="wd-laguage">
                                                        <?php
                                                        foreach (Configure::read('Languages') as $_language) {
                                                            if ($_language['code'] === $defaultLanguage) {
                                                                continue;
                                                            } ?>
                                                            <?php 
                                                                if(empty($_language['path'])){
                                                                    $flags = 'flags_default.png';                                                
                                                                }
                                                                else{$flags = $_language['path'];}                                                                                        
                                                            ?>
                                                <li><?php  echo $this->Html->link($this->Html->image('/img/' .$flags), array('action' => 'translate', $value['id'], $_language['code'], '?' => $_queries), array(
                                                                'class' => isset($_translateMaps[$value['id']][$_language['locale']]) ? '' : 'ui-state-disabled', 'escape' => false, 'title' => $_language['name']));?></li>           
                                                        <?php }?>
                                                        </ul>
                                                        
                                                    </div>
                                                </td>
                                            <?php endif; ?>

                                            <td class="table-col">
                                                <div class="table-actions">
                                                    <?php echo $this->Html->link('Edit', array('action' => 'edit', $value['id'],$theme_id,$defaultSubSite,'?' => $_queries), array('title' => __d('system', 'Edit this item'), 'class' => 'ui-icon ui-icon-pencil')); ?>
                                                    <?php
                                                    if ($value['custom']) {
                                                        echo $this->Html->link('Delete', array('action' => 'delete', '?' => array_merge($_queries, array('keys' => array($value['id'])))), array('title' => __d('system', 'Delete this item'), 'class' => 'ui-icon ui-icon-trash'), __d('system', 'Are you sure to delete #%s ?', $value['id']));
                                                    }
                                                    ?>
                                                </div>
                                            </td> 
                                        </tr>   
                                        <?php $_row++; ?>   
                                    <?php endforeach; ?>
                                <?php endif; ?>
                            <?php endforeach; ?>
                        </tbody>
                    </table>                   
                </div>
            </div>
        </div>
        <?php
        echo $this->Form->hidden('task', array('id' => 'task', 'value' => ''));                
        ?>        
        <div id="toolbar-control" class="control-icon">        
            <?php            
            echo $this->Form->end();
            ?>
        </div>
    </div>
    <!-- /Table -->
</div>
<script type="text/javascript">
    (function($){
        $(function(){
            $('.table-filter').find('select').change(function(){
                $(this).closest('form').submit();
            });            
        });
        $('#paginate').live('submit',function(){
            var _val = $('#FilterSubSite').val();
            if(_val==''){alert('Bạn chưa chọn sub site');return false;} 
            return true;                      
        })
    })(jQuery);
</script>
